
import { observable, action, toJS } from 'mobx';
import { WeaTools } from 'ecCom';
import { message, Modal } from 'antd';
import * as Apis from '../apis/customercard';
import * as CONTACTER_Apis from '../apis/cardManage';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { crmAddFormRules } from '../util/index';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class ContacterMapStore {
    @observable customerId = '';
    @observable mapDatas =[];
    @observable contacterNum = 0;
    @observable total_attitude = '';
    @observable canEdit = true;
    @observable rightMenu = [];

    @observable visible = false;
    @observable tableStore = new TableStore();
    dataKey = '';
    @observable loading = false;

    @observable operateObj = { operation: 'add' };

    @observable addContacterCondition = [];
    @observable addContacterform = new WeaForm();

    @observable verified = false;
    @observable hasRight = false; // 权限

    @action
    initDatas =(params = {}, callback) => {
      Apis.getContacterMap(params).then((data) => {
        if (!data.hasright && typeof (data.hasright) !== 'undefined') {
          this.hasRight = false;
          this.verified = true;
        } else {
          this.hasRight = true;
          this.verified = true;
          this.mapDatas = JSON.parse(data.datas.arrData);
          this.contacterNum = data.datas.contacterNum;
          this.total_attitude = data.datas.total_attitude;
          this.canEdit = data.datas.canEdit;
          this.rightMenu = data.datas.rightMenu;
          this.customerId = params.customerId;

          callback();
        }
      });
    }

    showMapLog = (params = {}) => {
      this.visible = true;
      this.loading = true;
      Apis.getContacterMapLogs({ customerId: this.customerId }).then((data) => {
        if (data.status == 'success') {
          this.tableStore.getDatas(data.sessionkey, params.current || 1);
          this.dataKey = data.sessionkey;
          this.loading = false;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    @action
    setVisible = (bool) => {
      this.visible = bool;
    }

    @action
    onShowColumn = () => {
      this.tableStore.setColSetVisible(true);
      this.tableStore.tableColSet(true);
    }

    @action
    saveContracterOperate =(params = {}, callback) => {
      Apis.contacterMapOperate(params).then((data) => {
        if (data.status == 'success') {
          this.initDatas({ customerId: params.customerId }, callback);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    @action
    resetContacterMap = (params = {}, callback) => {
      const _this = this;
      Modal.confirm({
        title: getLabel(15172, '系统提示'),
        content: getLabel(131519, '确定要恢复吗？'),
        onOk() {
          Apis.contacterMapOperate(params).then((data) => {
            if (data.status == 'success') {
              _this.initDatas({ customerId: params.customerId }, callback);
            } else {
              message.error(getLabel(83437, '出现错误！') + data.msgcode);
            }
          });
        },
        onCancel() {},
        okText: getLabel(33703, '确定'),
        cancelText: getLabel(201, '取消'),
      });
    }

    @action
    deleteContacterMap =(params = {}, callback) => {
      const _this = this;
      Modal.confirm({
        title: getLabel(15172, '系统提示'),
        content: getLabel(83600, '您确认要删除吗？'),
        okText: getLabel(33703, '确定'),
        cancelText: getLabel(201, '取消'),
        onOk() {
          CONTACTER_Apis.deleteContracter(params).then((data) => {
            if (data.status == 'success') {
              _this.initDatas({ customerId: params.customerId }, callback);
            } else {
              message.error(getLabel(83437, '出现错误！') + data.msgcode);
            }
          });
        },
        onCancel() {},
      });
    }

    @action
    addContacterForm = (params = {}) => {
      this.addContacterform = new WeaForm();
      this.operateObj = { ...this.operateObj, ...params };
      CONTACTER_Apis.getContacterAddForm(params).then((data) => {
        if (data.status == 'success') {
          this.addContacterCondition = data.datas;
          this.addContacterform && !this.addContacterform.isFormInit && this.addContacterform.initFormFields(crmAddFormRules(data.datas));
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    @action
    saveContacter = (params = {}, callback) => {
      if (this.operateObj.operation == 'add') {
        CONTACTER_Apis.saveContacterAdd({ ...this.operateObj, ...params }).then((data) => {
          if (data.status == 'success') {
            this.initDatas({ customerId: this.customerId }, callback);
            this.operateObj = { operation: 'add' };
          } else {
            message.error(getLabel(83437, '出现错误！') + data.msgcode);
          }
        });
      } else {
        Apis.contacterEditSave({ ...this.operateObj, ...params }).then((data) => {
          if (data.status == 'success') {
            this.initDatas({ customerId: this.customerId }, callback);
            this.operateObj = { operation: 'add' };
          } else {
            message.error(getLabel(83437, '出现错误！') + data.msgcode);
          }
        });
      }
    }
}
